package cn.home.bootstrap.publishsubscribe.direct;

import com.google.common.collect.Maps;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 发送端和场景3的区别：
 * 1、exchange的type为direct
 * 2、发送消息的时候加入了routing key
 */
@Slf4j
public class EmitLogDirect {
  private final static String IP = "127.0.0.1";
  private final static int  PORT = 5672;
  private static final String EXCHANGE_NAME = "direct_logs";

  public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(IP);
    factory.setPort(PORT);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    //配置交换器，为direct模式
    channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
    List<String> routeKeys = Arrays.asList("info","debug","error","warn");
    Map<String,String> messages = Maps.newHashMap();
    messages.put("info","every things is normal...");
    messages.put("debug","for test program...");
    messages.put("error","exception...");
    messages.put("warn","warning");
    for(String routeKey : routeKeys){
      String message = messages.get(routeKey);
      channel.basicPublish(EXCHANGE_NAME, routeKey, null, message.getBytes("UTF-8"));
      log.info(" [x] Sent routeKey:{}, message:{}" ,routeKey,message);
    }
    /*channel.close();
    connection.close();*/
  }
}

